package com.md.tw.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.md.tw.entity.Student;
import lombok.Data;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Data
public class StudentDaoImpl implements StudentDao {
    @Override
    public Student QueryStudentById(int id) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        DruidDataSource dataSource = context.getBean(DruidDataSource.class);
        try {
            DruidPooledConnection conn = dataSource.getConnection();
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery("select * from student where id = " + id);
            while (rs.next()) {
                Student stu = new Student(rs.getInt("id")
                        , rs.getString("stu_name")
                        , rs.getInt("stu_age")
                        , rs.getString("stu_gender"));
                return stu;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return null;
    }
}
